﻿using MediatR;
using T.Common.Domain.Common;
using RedDragon.ERP.API.Application.Commands.WorkCenter;
using RedDragon.Common.Domain.MaterialManagements;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.Common.Domain.warehouse;
using RedDragon.Common.Dtos;
using System.Collections.Generic;

namespace RedDragon.ERP.API.Application.Commands.Inventory
{
    public class QueryInventoryHandler : IRequestHandler<QueryInventoryCommand, Result<List<MaterialManagementDto>>>
    {
        /// <summary>
        /// 仓储仓储
        /// </summary>
        private readonly IBaseRepository<Warehouse> warehouseRepository;
        /// <summary>
        /// 物料清单仓储
        /// </summary>
        private readonly IBaseRepository<MaterialManagement> materialManagementRepository;
        /// <summary>
        /// 规格仓储
        /// </summary>
        private readonly IBaseRepository<SpecificationUnit> specificationUnitRepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<QueryInventoryHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="warehouseRepository"></param>
        /// <param name="materialManagementRepository"></param>
        /// <param name="logger"></param>
        public QueryInventoryHandler(IBaseRepository<Warehouse> warehouseRepository, IBaseRepository<MaterialManagement> materialManagementRepository, IBaseRepository<SpecificationUnit> specificationUnitRepository,ILogger<QueryInventoryHandler> logger)
        {
            this.warehouseRepository = warehouseRepository;
            this.materialManagementRepository = materialManagementRepository;
            this.specificationUnitRepository = specificationUnitRepository; 
            this.logger = logger;
        }

        /// <summary>
        /// 打印库存清单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result<List<MaterialManagementDto>>> Handle(QueryInventoryCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<List< MaterialManagementDto>> ();
            try
            {
                var warehouselist = await (await warehouseRepository.GetAll()).ToListAsync();
                var materialManagementlist = await (await materialManagementRepository.GetAll()).ToListAsync();
                var list =(from a in warehouselist
                            join b in materialManagementlist
                            on a.Serialnumber equals b.Serialnumber
                           //where (a.WarehouseId == request.WarehouseId&& b.MaterialId == request.MaterialId)
                            select new MaterialManagementDto()
                            {
                                WarehouseId = a.WarehouseId,
                                WarehousNumBer = a.WarehousNumBer,
                                WarehouseName = a.WarehouseName,
                                MaterialId = b.MaterialId,
                                MaterialsServicesId = b.MaterialsServicesId,
                                MaterialCode = b.MaterialCode,
                                MaterialName = b.MaterialName, 
                                MaterialCategoryId = b.MaterialCategoryId,
                                MaterialUnitId = b.MaterialUnitId,
                                Specifications = b.Specifications,
                                SpecificationUnitId = b.SpecificationUnitId,
                                PackagingSpecifications = b.PackagingSpecifications,
                                ISInspection = b.ISInspection,
                                InventoryLimit = b.InventoryLimit,
                                InventoryLowerLimit = b.InventoryLowerLimit,
                                InventoryAccount = b.InventoryAccount,
                                SalesRevenueAccount = b.SalesRevenueAccount,
                                SalesCostAccount = b.SalesCostAccount,
                                MaterialQRCode = b.MaterialQRCode,
                                Serialnumber = b.Serialnumber,
                            }).ToList();
                if (request.WarehouseId != 0)
                {
                    list = list.Where(d => d.WarehouseId == request.WarehouseId).ToList();
                }
                if (request.MaterialId != 0)
                {
                    list = list.Where(d => d.MaterialId == request.MaterialId).ToList();
                }
                result.Data = list;
                result.Msg = "库存清单信息获取成功";
            }
            catch (Exception ex)
            {
                result.Msg = "库存清单信息获取失败" + ex.Message;
                logger.LogError("库存清单信息获取失败" + ex);
                throw;
            }
            return result;
        }
    }
}
